home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / t_os / wink24 / src / wc2.c < prev    next >
Text File  |  1993-07-08  |  9KB  |  379 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <ctype.h>
  4. #include    <strings.h>
  5. #include    <msdos.cf>
  6. /* ------------------ Debug  91.08.28 Pumpkin --------------- */
  7. #include    <dos.h>
  8. /* ---------------------------------------------------------- */
  9.  
  10. #define SHORT   short int
  11. #define LONG    long int
  12. #define ULONG   unsigned long int
  13. #define BOOL    int
  14.  
  15. #define TRUE    1
  16. #define FALSE   0
  17. #define ERR     (-1)
  18.  
  19. #define    MAXWCP    300
  20.  
  21. typedef struct {
  22.     SHORT   wb_dsp;
  23.     SHORT   wb_att;
  24.     SHORT   wb_time,wb_date;
  25.     ULONG   wb_size;
  26.     char    wb_name[14];
  27. } WCBUF;
  28.  
  29. extern void    wrtstr();
  30. extern void    Dmy_form();
  31. extern int    getch();
  32. extern void    Dsp_vram_flash();
  33.  
  34. static int    wcmax=0;
  35. static WCBUF    *topwcp=NULL;
  36. static char    dir[80];
  37. static char     wild[80];
  38.  
  39. char    *endname(char *name)
  40. {
  41.     char    *p;
  42.  
  43.     for ( p=name ; *p != '\0' ; p++ );
  44.     for ( ; p != name && *p != '\\' && *p != ':' ; p-- );
  45.     if ( p != name )
  46.         p++;
  47.     return p;
  48. }
  49. char    *subname(char *name)
  50. {
  51.     static char tmp[14];
  52.     int        i;
  53.     char    *p;
  54.  
  55.     if ( *name == '.' ) {
  56.     for ( i = 0,p = tmp ; i < 9 ; i++ ) {
  57.         if ( *name == '\0' )
  58.         *(p++) = ' ';
  59.         else
  60.         *(p++) = *(name++);
  61.     }
  62.     } else {
  63.     for ( i = 0,p = tmp ; i < 9 ; i++ ) {
  64.         if ( *name == '.' || *name == '\0' )
  65.         *(p++) = ' ';
  66.         else
  67.         *(p++) = *(name++);
  68.     }
  69.     if ( *name == '.' ) name++;
  70.     }
  71.     for ( ; i < 12 ; i++ ) {
  72.     if ( *name == '\0' )
  73.         *(p++) = ' ';
  74.     else
  75.         *(p++) = *(name++);
  76.     }
  77.     *p = '\0';
  78.     return tmp;    
  79. }
  80. int    cmpname(char *arg,char *str)
  81. {
  82.     int     i;
  83.     char    *p,tmp[14];
  84.  
  85.     strcpy(tmp,subname(arg));
  86.     p = subname(str);
  87.     if ( (i = strcmp(tmp + 9,p + 9)) != 0 )
  88.     return i;
  89.     tmp[9] = p[9] = '\0';
  90.     return strcmp(tmp,p);
  91. }
  92. void    sort(int bs,int mx)
  93. {
  94.     int     i,j;
  95.     WCBUF   x,w;
  96.  
  97.     i=bs;
  98.     j=mx;
  99.     memcpy(&x,&(topwcp[bs + (mx - bs) / 2]),sizeof(WCBUF));
  100.     do {
  101.         while ( cmpname(topwcp[i].wb_name, x.wb_name) < 0 )
  102.             i++;
  103.         while ( cmpname(topwcp[j].wb_name, x.wb_name) > 0 )
  104.             j--;
  105.         if ( i <= j ) {
  106.             memcpy(&w,&(topwcp[i]),sizeof(WCBUF));
  107.             memcpy(&(topwcp[i]),&(topwcp[j]),sizeof(WCBUF));
  108.             memcpy(&(topwcp[j]),&w,sizeof(WCBUF));
  109.             i++; j--;
  110.         }
  111.     } while ( i <= j );
  112.     if ( bs < j )
  113.         sort(bs,j);
  114.     if ( i < mx )
  115.         sort(i,mx);
  116. }
  117. BOOL    subwc(char *arg,int mode,WCBUF *wcp)
  118. {
  119.     static BOOL opflg=FALSE;
  120.     static struct {
  121.         char    dd_dmy[22];
  122.         SHORT   dd_time,dd_date;
  123.         LONG    dd_size;
  124.         char    dd_name[13];
  125.     } dma;
  126.  
  127.     if ( opflg == FALSE ) {
  128.         Registers.AX.R = 0x1A00;
  129.         Registers.DX.R = (int)&dma;
  130.         Registers.DS.R = getds();
  131.         calldos(); 
  132.         Registers.AX.R = 0x4E00;
  133.         Registers.CX.R = mode;
  134.         Registers.DX.R = (int)arg;
  135.         Registers.DS.R = getds();
  136.         opflg = TRUE;
  137.     }
  138.     else
  139.         Registers.AX.R = 0x4F00;
  140.  
  141.     calldos();
  142.     if ( (Registers.Flags & 0x0001) != 0 ) {
  143.         opflg = FALSE;
  144.     return FALSE;
  145.     }
  146.  
  147.     wcp->wb_dsp = 0;
  148.     wcp->wb_att = dma.dd_dmy[21];
  149.     wcp->wb_date = dma.dd_date;
  150.     wcp->wb_time = dma.dd_time;
  151.     wcp->wb_size = dma.dd_size;
  152.     strcpy(wcp->wb_name,dma.dd_name);
  153.     return TRUE;
  154. }
  155. void    chdir(char *name)
  156. {
  157.     char tmp[129],i;
  158.     strcpy(tmp,name); i = strlen(tmp);
  159.     if (i > 3 && tmp[i-1] == '\\') tmp[i-1] = 0;
  160.  
  161.     Registers.AX.R = 0x3B00;
  162.     Registers.DX.R = (int)name;
  163.     Registers.DS.R = getds();
  164.     calldos();
  165.     if ( name[1] == ':' ) {
  166.     Registers.AX.R = 0x0E00;
  167.     Registers.DX.R = (*name & 0xDF) - 'A';
  168.     calldos();
  169.     }
  170. }
  171. /* ------------------ Debug  92.02.07 Pumpkin --------------- */
  172. int    diskReady(int drive)
  173. {
  174.     Registers.AX.R = 0x3600;
  175.     Registers.DX.R = drive;
  176.     calldos();
  177.     if (Registers.AX.R == 0xffff) return ERR;
  178.     return FALSE;
  179. }
  180. /* ---------------------------------------------------------- */
  181. int    retdir(char *name)
  182. {
  183. /* ------------------ Debug  92.02.07 Pumpkin --------------- */
  184.     if (diskReady(0) == ERR) {
  185.     *(long *)name = 0;
  186.     return ERR;
  187.     }
  188. /* ---------------------------------------------------------- */
  189.  
  190.     Registers.AX.R = 0x4700;
  191.     Registers.DX.R = 0x0000;
  192.     Registers.SI.R = (int)name+3;
  193.     Registers.DS.R = getds();
  194.     calldos();
  195.  
  196.     Registers.AX.R = 0x1900;
  197.     calldos();
  198.  
  199.     *(name++) = 'A' + (Registers.AX.R & 0xFF);
  200.     *(name++) = ':';
  201.     *(name++) = '\\';
  202.     if ( *name != '\0' )
  203.     strcat(name,"\\");
  204.     return (Registers.AX.R & 0xFF);
  205. }
  206. int    WC_open(char *file)
  207. {
  208.     int     i;
  209.     char    *p,*s;
  210.  
  211.     if ( (s = endname(file)) != file ) {
  212.     for ( p = dir ; file != s ; )
  213.         *(p++) = *(file++);
  214.     /* ------------------ Debug  91.08.28 Pumpkin --------------- */
  215.     if (*(p-2) != ':'  && *(p-1) == '\\' ) p--;
  216. /****
  217.     if ( *(p-1) == '\\' ) p--;
  218. ****/
  219.     /* ---------------------------------------------------------- */
  220.     *p = '\0';
  221.     chdir(dir);
  222.     }
  223.     strcpy(wild,s);
  224.  
  225.     if ( topwcp == NULL &&
  226.     (topwcp = (WCBUF *)malloc(sizeof(WCBUF)*MAXWCP)) == NULL )
  227.     return 0;
  228.     wcmax = 0;
  229.     while ( wcmax < MAXWCP && subwc("*.*",0x10,&topwcp[wcmax]) != FALSE ) {
  230.     if ( (topwcp[wcmax].wb_att & 0x10) != 0 )
  231.         wcmax++;
  232.     }
  233.     if ( wcmax > 1 ) sort(0,wcmax - 1);
  234.     i = wcmax;
  235.     while ( wcmax < MAXWCP && subwc(wild,0x21,&topwcp[wcmax]) != FALSE )
  236.     wcmax++;
  237.     if ( (wcmax - 1) > i ) sort(i,wcmax - 1);
  238.  
  239.     retdir(dir);
  240.     return wcmax;
  241. }
  242. void    File_dsp(off,no)
  243. int    off,no;
  244. {
  245.     int     i,c;
  246.     char    *p;
  247.  
  248.     for ( i = 0 ; i < 30 ; i++ ) {
  249.     c = 0x05;
  250.     if ( (i+off) >= wcmax )
  251.         p = "            ";
  252.     else {
  253.         p = subname(topwcp[i+off].wb_name);
  254.         if ( (topwcp[i+off].wb_att & 0x10) != 0 )
  255.             c = 0x0D;
  256.         if ( (i+off) == no )
  257.         c |= 0x10;
  258.     }
  259.     wrtstr(p,17+(i%3)*14,11+(i/3),c);
  260.     }
  261. }
  262. int    File_sel(char *file)
  263. {
  264.     int     i,no,off,fg,ch;
  265.     int        no_s,off_s;
  266.     char    tmp[160];
  267.     char    *p;
  268.     char    bkdir[80];
  269.  
  270.  
  271.     /* ------------------ Debug  91.08.28 Pumpkin --------------- */
  272.     unsigned int att;
  273.  
  274.     if ( *file == '\0' ) {
  275.     strcpy(file,"*.*");
  276.     } else {
  277.         p = file + strlen(file) - 1;
  278.         if ( *p == ':' || *p == '\\' ) {
  279.         strcat(file,"*.*");
  280.     } else {
  281.         if ((_dos_getfileattr(file,&att) == 0) && ((att & 16) == 16)) 
  282.             strcat(file,"\\*.*");
  283.     }
  284.     }
  285.     if (strcmp(file,"\\*.*") == 0) {
  286.     retdir(bkdir);
  287.     strncpy(tmp,bkdir,2);
  288.     strcat(tmp,file);
  289.     strcpy(file,tmp);
  290.     }
  291.  
  292. /*****
  293.     if ( *file == '\0' )
  294.     strcpy(file,"*.*");
  295. *****/
  296.     /* ---------------------------------------------------------- */
  297.  
  298.     for ( p = file ; *p != '*' && *p != '?' && *p != '\0' ; p++ );
  299.     if ( *p == '\0' )
  300.     return FALSE;
  301.  
  302.     retdir(bkdir);
  303.  
  304.  
  305.     /* ------------------ New file  91.10.12 Pumpkin ------------ */
  306.     if ( WC_open(file) == 0 )
  307.     return 2;
  308. /*****
  309.     if ( WC_open(file) == 0 )
  310.     return FALSE;
  311. *****/
  312.     /* ---------------------------------------------------------- */
  313.  
  314.  
  315.     Dmy_form(tmp,44,0x98,0x95,0x99); 
  316.     wrtstr(tmp,15,8,0x05);
  317.  
  318.     Dmy_form(tmp,44,0x96,0x20,0x96); 
  319.     for ( i = 0 ; i < 12 ; i++ )
  320.         wrtstr(tmp,15,9+i,0x05);
  321.  
  322.     Dmy_form(tmp,44,0x9A,0x95,0x9B); 
  323.     wrtstr(tmp,15,21,0x05);
  324.  
  325.     Dmy_form(tmp,44,0x93,0x95,0x92); 
  326.     wrtstr(tmp,15,10,0x05);
  327.  
  328.     Dmy_form(tmp,44,0x96,0x20,0x96); 
  329.     wrtstr(dir,17,9,0x05);
  330.  
  331.     for ( no_s = off_s = 1,no = off = 0,fg = FALSE ; fg == FALSE ; ) { 
  332.     if ( no != no_s || off != off_s ) {
  333.             File_dsp(off,no);
  334.         no_s = no; off_s = off;
  335.     }
  336.     ch = getch();
  337.     if ( ch == '\x0D' ) {
  338.         if ( (topwcp[no].wb_att & 0x10) != 0 ) {
  339.         strcat(dir,topwcp[no].wb_name);
  340.         strcat(dir,"\\");
  341.         strcat(dir,wild);
  342.         WC_open(dir);
  343.                 wrtstr(tmp,15,9,0x05);
  344.                 wrtstr(dir,17,9,0x05);
  345.         no_s = off_s = 1;
  346.         no = off = 0;
  347.         } else {
  348.         fg = TRUE;
  349.         }
  350.     } else if ( ch == '\x1B' ) {
  351.         fg = ERR;
  352.     } else if ( ch == '\x1C' && no < (wcmax-1) ) {
  353.         no++;
  354.     } else if ( ch == '\x1D' && no > 0 ) {
  355.         no--;
  356.     } else if ( ch == '\x1F' && (no + 3) < wcmax ) {
  357.         no += 3;
  358.     } else if ( ch == '\x1E' && no >= 3 ) {
  359.         no -= 3;
  360.     }
  361.     while ( no < off ) off -= 3;
  362.     while ( no >= (off+30) ) off += 3;
  363.     }
  364.     Dsp_vram_flash();
  365.     if ( fg == TRUE ) {
  366.     strcat(dir,topwcp[no].wb_name);
  367.     strcpy(file,dir);
  368.     fg = FALSE;
  369.     }
  370.     free(topwcp);
  371.     topwcp = NULL;
  372.  
  373.     if ( bkdir[3] != '\0' )
  374.     bkdir[strlen(bkdir)-1] = '\0';
  375.     chdir(bkdir);
  376.  
  377.     return fg;
  378. }
  379.